// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   SelectTag.java

package org.apache.struts.taglib.html;

import java.lang.reflect.InvocationTargetException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.util.*;

// Referenced classes of package org.apache.struts.taglib.html:
//            BaseHandlerTag

public class SelectTag extends BaseHandlerTag
{

    protected String match[];
    protected static MessageResources messages = MessageResources.getMessageResources("org.apache.struts.taglib.html.LocalStrings");
    protected String multiple;
    protected String name;
    protected String property;
    protected String saveBody;
    protected String size;
    protected String value;

    public SelectTag()
    {
        match = null;
        multiple = null;
        name = "org.apache.struts.taglib.html.BEAN";
        property = null;
        saveBody = null;
        size = null;
        value = null;
    }

    public String getMultiple()
    {
        return multiple;
    }

    public void setMultiple(String multiple)
    {
        this.multiple = multiple;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getSize()
    {
        return size;
    }

    public void setSize(String size)
    {
        this.size = size;
    }

    public boolean isMatched(String value)
    {
        if(match == null || value == null)
            return false;
        for(int i = 0; i < match.length; i++)
            if(value.equals(match[i]))
                return true;

        return false;
    }

    public String getProperty()
    {
        return property;
    }

    public void setProperty(String property)
    {
        this.property = property;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }

    public int doStartTag()
        throws JspException
    {
        ResponseUtils.write(pageContext, renderSelectStartElement());
        pageContext.setAttribute("org.apache.struts.taglib.html.SELECT", this);
        calculateMatchValues();
        return 2;
    }

    protected String renderSelectStartElement()
        throws JspException
    {
        StringBuffer results = new StringBuffer("<select");
        results.append(" name=\"");
        if(indexed)
            prepareIndex(results, name);
        results.append(property);
        results.append("\"");
        if(accesskey != null)
        {
            results.append(" accesskey=\"");
            results.append(accesskey);
            results.append("\"");
        }
        if(multiple != null)
            results.append(" multiple=\"multiple\"");
        if(size != null)
        {
            results.append(" size=\"");
            results.append(size);
            results.append("\"");
        }
        if(tabindex != null)
        {
            results.append(" tabindex=\"");
            results.append(tabindex);
            results.append("\"");
        }
        results.append(prepareEventHandlers());
        results.append(prepareStyles());
        results.append(">");
        return results.toString();
    }

    private void calculateMatchValues()
        throws JspException
    {
        if(value != null)
        {
            match = new String[1];
            match[0] = value;
        } else
        {
            Object bean = RequestUtils.lookup(pageContext, name, null);
            if(bean == null)
            {
                JspException e = new JspException(messages.getMessage("getter.bean", name));
                RequestUtils.saveException(pageContext, e);
                throw e;
            }
            try
            {
                match = BeanUtils.getArrayProperty(bean, property);
                if(match == null)
                    match = new String[0];
            }
            catch(IllegalAccessException e)
            {
                RequestUtils.saveException(pageContext, e);
                throw new JspException(messages.getMessage("getter.access", property, name));
            }
            catch(InvocationTargetException e)
            {
                Throwable t = e.getTargetException();
                RequestUtils.saveException(pageContext, t);
                throw new JspException(messages.getMessage("getter.result", property, t.toString()));
            }
            catch(NoSuchMethodException e)
            {
                RequestUtils.saveException(pageContext, e);
                throw new JspException(messages.getMessage("getter.method", property, name));
            }
        }
    }

    public int doAfterBody()
        throws JspException
    {
        if(bodyContent != null)
        {
            String value = bodyContent.getString();
            if(value == null)
                value = "";
            saveBody = value.trim();
        }
        return 0;
    }

    public int doEndTag()
        throws JspException
    {
        pageContext.removeAttribute("org.apache.struts.taglib.html.SELECT");
        StringBuffer results = new StringBuffer();
        if(saveBody != null)
            results.append(saveBody);
        results.append("</select>");
        ResponseUtils.write(pageContext, results.toString());
        return 6;
    }

    public void release()
    {
        super.release();
        match = null;
        multiple = null;
        name = "org.apache.struts.taglib.html.BEAN";
        property = null;
        saveBody = null;
        size = null;
        value = null;
    }

}
